package cn.edu.scau.cmi.zhonghaozheng.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.zhonghaozheng.composite.safe.Organization;
import cn.edu.scau.cmi.zhonghaozheng.composite.safe.OrganizationComponent;
import cn.edu.scau.cmi.zhonghaozheng.composite.safe.Person;
import javafx.event.ActionEvent;

import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;

public class CompositeController implements Initializable{
	@FXML
	private Button btn_add;
	@FXML
	private TextField textfield_parent;
	@FXML
	private TextField textfield_child;
	@FXML
	private TextField textfield_remove;
	@FXML
	private Button btn_remove;
	@FXML
	private RadioButton radio_org;
	@FXML
	private ToggleGroup togglegroup;
	@FXML
	private RadioButton radio_per;
	@FXML
	private TreeView treeView;
	Organization rootOrganization = new Organization("ROOT");
	List<Organization> orgList = new ArrayList<Organization>();
	List<Person> perList = new ArrayList<Person>();
	List<myTreeItem> treeList = new ArrayList<myTreeItem>();
	Queue<OrganizationComponent> queue = new LinkedList<OrganizationComponent>();

	@FXML
	public void handleBtnAdd(ActionEvent event) {
		String parentName = textfield_parent.getText();
		String childName = textfield_child.getText();
		Iterator<OrganizationComponent> rootOrgIter = rootOrganization.getChildren().iterator();
		Iterator<Organization> orgIter = orgList.iterator();
		Iterator<Person> perIter = perList.iterator();
		if((!rootOrgIter.hasNext()||parentName.equals(""))&&!childName.equals("")) {
			System.out.println("create org");
			OrganizationComponent child = getSelectedTypeChild(childName);
			rootOrganization.addChildren(child);
			child.setParent(rootOrganization);
			myTreeItem<String> ti = new myTreeItem<String>(childName);
			ti.setMyTreeItemParent(null);
			treeList.add(ti);
			if(radio_org.isSelected()) {
				orgList.add((Organization) child);
			}
			else if(radio_per.isSelected()) {
				perList.add((Person) child);
			}
		}
		else {
			System.out.println("create branch");
			Organization parent = null;
			OrganizationComponent child = getSelectedTypeChild(childName);
			while(orgIter.hasNext()) {
				parent = orgIter.next();
				if(((Organization)parent).getName().equals(parentName)) {
					myTreeItem<String> ti = null;
					((Organization)parent).addChildren(child);
					child.setParent(parent);
					if(radio_org.isSelected()) {
						orgList.add((Organization) child);
						ti = new myTreeItem<String>(((Organization)child).getName());
					}
					else if(radio_per.isSelected()) {
						perList.add((Person) child);
						ti = new myTreeItem<String>(((Person)child).getName());
					}
					Iterator<myTreeItem> treeIter = treeList.iterator();
					while(treeIter.hasNext()) {
						myTreeItem treeparent = treeIter.next();
						System.out.println(treeparent.getValue());
						if(treeparent.getValue().equals(((Organization)parent).getName())) {
							ti.setMyTreeItemParent(treeparent);
						}
					}
					treeList.add(ti);
					break;
				}
			}
			if(parent == null) {
				System.out.println("父组织不存在");
			}
		}
//		showOrganizationTree();
		showTree();
	}

	private OrganizationComponent getSelectedTypeChild(String childName) {
		OrganizationComponent child = null;
		if(radio_org.isSelected()) {
			child = new Organization(childName);
		}
		else if(radio_per.isSelected()) {
			child = new Person(childName);
		}
		return child;
	}
	// Event Listener on Button[#btn_remove].onAction
	@FXML
	public void handleBtnRemove(ActionEvent event) {
		String removeName = textfield_remove.getText();
		Iterator<OrganizationComponent> rootOrgIter = rootOrganization.getChildren().iterator();
		Iterator<Organization> orgIter = orgList.iterator();
		Iterator<Person> perIter = perList.iterator();
		System.out.println("remove");
		OrganizationComponent removeObject = null;
		while(orgIter.hasNext()) {
			removeObject = orgIter.next();
			if(((Organization)removeObject).getName().equals(removeName)) {
				orgList.remove(removeObject);
				removeObject.getParent().removeChildren(removeObject);
				Iterator<myTreeItem> treeIter = treeList.iterator();
				while(treeIter.hasNext()) {
					myTreeItem<String> removeTree = null;
					removeTree = treeIter.next();
					if(removeTree.getValue().equals(removeName)) {
						removeTree.getParent().getChildren().remove(removeTree);
						treeList.remove(removeTree);
						break;
					}
				}
				break;
			}
		}
		while(perIter.hasNext()) {
			removeObject = perIter.next();
			if(((Person)removeObject).getName().equals(removeName)) {
				perList.remove(removeObject);
				removeObject.getParent().removeChildren(removeObject);
				Iterator<myTreeItem> treeIter = treeList.iterator();
				while(treeIter.hasNext()) {
					myTreeItem<String> removeTree = null;
					removeTree = treeIter.next();
					if(removeTree.getValue().equals(removeName)) {
						removeTree.getParent().getChildren().remove(removeTree);
						treeList.remove(removeTree);
						break;
					}
				}
				break;
			}
		}
		if(removeObject == null) {
			System.out.println("父组织不存在");
		}
		showTree();
	}

	private void showTree() {
		myTreeItem<String> tree = new myTreeItem<String>("");
		tree.setExpanded(true);
		treeView.setRoot(tree);
		Iterator<myTreeItem> treeIter = treeList.iterator();
		while(treeIter.hasNext()) {
			myTreeItem<String> temp = treeIter.next();
			if(temp.getMyTreeItemParent()==null) {
				tree.getChildren().add(temp);
			}
			else if(!temp.getMyTreeItemParent().getChildren().contains(temp)){
				temp.getMyTreeItemParent().getChildren().add(temp);
			}
			temp.setExpanded(true);
		}
	}	
	
	private void showOrganizationTree() {
		myTreeItem<String> tree = new myTreeItem<String>("");
		treeView.setRoot(tree);
		Iterator<myTreeItem> treeIter = treeList.iterator();
//		while(treeIter.hasNext()) {
//			System.out.println(treeIter.next());
//		}
		Iterator<OrganizationComponent> iter = rootOrganization.getChildren().iterator();
		queue.offer(rootOrganization);
		while(!queue.isEmpty()) {
			iter = ((Organization)queue.poll()).getChildren().iterator();
			while(iter.hasNext()) {
				OrganizationComponent temp = iter.next();
				
				if(temp.getType() == 0) {
					queue.offer(temp);
				}
			}

		}

	}
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
}
